﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using System.Data.Entity; // needed for lambda includes

using TheCup.ApiControllers;

namespace TheCup.ApiControllers
{
    public class MatchPlayController : ApiController
    {
        TheCupContext ctx = new TheCupContext();

        //public IEnumerable<MatchPlayResult> Get()
        //{
        //    List<MatchPlayResult> e = new List<MatchPlayResult>();
        //    e = (from ev in ctx.MatchPlayResults.Include(p1 => p1.Player1).Include(p2 => p2.Player2).Include(w => w.WinningPlayer)
        //         select ev).OrderByDescending(dt => dt.Date).ToList<MatchPlayResult>();
        //    return e;
        //}

        public IEnumerable<MatchPlayResult> GetAllMatchesForPlayer(int playerid)
        {
            List<MatchPlayResult> e = new List<MatchPlayResult>();
            e = (from ev in ctx.MatchPlayResults.Include(p1 => p1.Player1).Include(p2 => p2.Player2).Include(w => w.WinningPlayer)
                 where ev.Player1Id == playerid || ev.Player2Id == playerid
                 select ev).OrderByDescending(dt => dt.Date).ToList<MatchPlayResult>();
            return e;
        }

        //public IEnumerable<MatchPlayResult> GetMatchesPlayedForSeason(DateTime date, int PlayerId)
        //{
        //    List<MatchPlayResult> e = new List<MatchPlayResult>();
        //    ApiControllers.SeasonController c = new SeasonController();
        //    Season s = c.Get(date);
        //    if (s != null)
        //    {
        //        e = (from ev in ctx.MatchPlayResults.Include(p1 => p1.Player1).Include(p2 => p2.Player2).Include(w => w.WinningPlayer)
        //             where ev.Date >= s.StartDate && ev.Date <= s.EndDate && (ev.Player1Id == PlayerId || ev.Player2Id == PlayerId)
        //             select ev).ToList<MatchPlayResult>();
        //    }
        //    return e;
        //}
        
        public IEnumerable<Player> GetPlayersNotPlayedForSeason(DateTime date, int playerid)
        {
            List<MatchPlayResult> e = new List<MatchPlayResult>();
            ApiControllers.SeasonController c = new SeasonController();
            Season s = c.Get(date);
            if (s != null)
            {
                e = (from ev in ctx.MatchPlayResults.Include(p1 => p1.Player1).Include(p2 => p2.Player2).Include(w => w.WinningPlayer)
                     where ev.Date >= s.StartDate && ev.Date <= s.EndDate && (ev.Player1Id == playerid || ev.Player2Id == playerid)
                     select ev).ToList<MatchPlayResult>();
            }

            // get a distinct list of earlier opponents
            List<Player> playersPlayed = new List<Player>();
            playersPlayed.AddRange(e.Select(p1 => p1.Player1));
            playersPlayed.AddRange(e.Select(p1 => p1.Player2));
            playersPlayed = playersPlayed.Distinct(new ComparePlayers()).Where(pl => pl.Id != playerid).ToList<Player>();

            // get active players
            ApiControllers.PlayerController pc = new PlayerController();
            IEnumerable<Player> missingPlayers = pc.Get();

            missingPlayers = missingPlayers.Where(pl => pl.Id != playerid).Except(playersPlayed, new ComparePlayers());

            return missingPlayers;
        }

        public IEnumerable<MatchPlayResult> GetSeason(int id)
        {
            List<MatchPlayResult> e = new List<MatchPlayResult>();
            ApiControllers.SeasonController c = new SeasonController();

            Season s = c.Get(id);
            if (s == null)
            {
                // try get for current date
                s = c.Get(DateTime.UtcNow);
            }

            if (s != null)
            {
                e = (from ev in ctx.MatchPlayResults.Include(p1 => p1.Player1).Include(p2 => p2.Player2).Include(w => w.WinningPlayer)
                     where ev.Date >= s.StartDate && ev.Date <= s.EndDate
                     select ev).ToList<MatchPlayResult>();
            }
            return e;
        }
                
        // POST api/<controller>
        [Authorize]
        public void Post([FromBody]MatchPlayResult e)
        {
            e.WhenCreated = DateTime.UtcNow;
            ctx.MatchPlayResults.Add(e);
            ctx.SaveChanges();
        }

        [Authorize]
        public void Put(int id, [FromBody]string value)
        {
        }

        [Authorize]
        public void Delete(int id)
        {
            using (var ctx = new TheCupContext())
            {
                var mp = ctx.MatchPlayResults.Find(id);
                ctx.MatchPlayResults.Remove(mp);
                ctx.SaveChanges();
            }
            
        }
    }

}